package com.tr.rentcar.controller.loginandregister;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.dao.UserDao;

@WebServlet("/logout")
public class LogoutController extends HttpServlet {

	/**
	 * 注销用户登录
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			
			User user=(User) req.getSession().getAttribute("userinfo");
			//修改登录状态表用户的状态为下线
			if(user!=null){
				boolean ok=new UserDao().updateLoginStatus(user.getUid());
				String status=req.getParameter("status");
				if("1".equals(status)){
					resp.getWriter().println(ok);
				}else{
					//首先移除session中的user
					req.getSession().removeAttribute("userinfo");
					//移除cookie
					Cookie cookie=new Cookie("autologin","");
					cookie.setMaxAge(0);
					cookie.setPath(req.getContextPath());
					resp.addCookie(cookie);
					//注销后跳转到登录页面
					resp.sendRedirect(req.getContextPath()+"/login");
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
